from django.contrib.auth.models import User
from django.db import models

# Create your models here.
from utils.modelsMixin import DateTimeModelsMixin, ModelsSetMixin


class UserDetail(DateTimeModelsMixin):
    SEX_CHOICES = {
        (0, '女'),
        (1, '男'),

    }
    avatrar = models.TextField(null=True, blank=True, verbose_name='头像')
    phone = models.CharField(max_length=11, unique=True, null=True, blank=True, verbose_name='电话号码')
    age = models.IntegerField(null=True, blank=True, verbose_name='年龄')
    sex = models.IntegerField(null=True, blank=True, verbose_name='性别', choices=SEX_CHOICES)
    birthday = models.DateField(null=True, blank=True, verbose_name='生日')

    user = models.OneToOneField(User, on_delete=models.CASCADE)
    # 关联的收货地址被删了，默认收货地址设置为空
    default_address = models.ForeignKey('Address', null=True, blank=True, on_delete=models.SET_NULL,
                                        verbose_name='默认收货地址')

    class Meta:
        db_table = 'user_detail'
        verbose_name = '用户详情'
        verbose_name_plural = verbose_name


class Area(models.Model):  # 省市区表
    name = models.CharField(max_length=20, verbose_name='名称')
    # on_delete=models.SET_NULL 如果上级被删了就没有上级，设置为空
    parent = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True, verbose_name='上级行政区划')

    class Meta:
        db_table = 'area'
        verbose_name = '行政区划'
        verbose_name_plural = verbose_name


class Address(ModelsSetMixin):  # 收货地址表
    # related_name修改set管理器的名字
    name = models.CharField(max_length=40, verbose_name='地址名')
    province = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='省', related_name='province_addresses')
    city = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='市', related_name='city_addresses')
    district = models.ForeignKey(Area, on_delete=models.PROTECT, verbose_name='区', related_name='district_addresses')
    place = models.CharField(max_length=40, verbose_name='详情地址')
    receiver = models.CharField(max_length=40, verbose_name='收货人')
    mobile = models.CharField(max_length=40, verbose_name='手机')

    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='用户')

    class Meta:
        ordering = ['-update_time']
        db_table = 'address'
        verbose_name = '收货地址'
        verbose_name_plural = verbose_name
